<?php
class AppController extends Controller {

	//var $view = 'Smarty';
	//var $theme='smarty';
	var $theme='dzstyle';
	var $view = 'Dzstyle';
	var $helpers = array(
        'Html','Section','Form','Javascript','Layout','Ckeditor',
	);
	
	var $components = array(
        'Acl',
        'Auth',
		'Acl.AclFilter',
        'RequestHandler',
        'Email',
    );
    var $uses = array('Setting','Category');
    
    var $currentUser = null;

	function beforeFilter() {
		$this->currentUser = $this->Auth->user();
		
		
		$this->Auth->allowedActions = array('login','admin_login','view','register');
		if($this->currentUser['User']['id'])
		{
			$this->AclFilter->auth();
		}
		
		//$this->Auth->allowedActions = array('*');
		//print_r($this->Session->read('Auth.User')); 
		//exit;	
		$this->Setting->writeConfiguration();
		
		$site_info = Configure::read('Site');
		$this->set('site', $site_info);
		$this->set('seokeywords', $site_info['seokeywords']);		
		$this->set('seodescription', $site_info['seodescription']);
		
		
		if (isset($this->params['admin'])) {
            $this->layout = 'admin';
        }
		if ($this->RequestHandler->isAjax()) {
            $this->layout = 'ajax';
        }
        
		if (isset($this->params['locale'])) {
            Configure::write('Config.language', $this->params['locale']);
        }
        
        $this->set('top_cateid', '');
        $this->pageTitle ='EasyWeb';
        $this->set('pageTitle', 'EasyWeb');
        
	}
	
	function beforeRender()
	{
		//echo '<pre>';	
		$this->set('CurrentUser', Set::map($this->Auth->user()));
	}
// 后台增加 
	function admin_index() {
		$this->pageTitle = __("Add ".$this->modelClass, true);
				
        $this->paginate[$this->modelClass]['order'] = $this->modelClass.'.id DESC';
		//$this->paginate[$this->modelClass]['limit'] = Configure::read('Reading.nodes_per_page');
		${$this->name} = $this->paginate($this->modelClass);
		$this->set(compact($this->name));
    }
	// 后台增加 
	function admin_add() {
		$this->pageTitle = __("Add ".$this->modelClass, true);
		
		$modelClass = $this->modelClass;
        if (!empty($this->data)) {
            $this->{$modelClass}->create();
                  
            if ($this->{$modelClass}->save($this->data)) {
                $this->Session->setFlash(__('The User has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
                
            }
        }
    }
    // 后台修改
 	function admin_edit($id = null) {
 		$this->pageTitle = __("Edit ".$this->modelClass, true);
 		$modelClass = $this->modelClass;
 		//print_r($this->params);
 		//print_r($this->data);exit;
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('Invalid User', true));
            $this->redirect(array('action' => 'index'));
        }
        
        $categories = $this->Category->generatetreelist();
		//$parents[''] = 'root';
		$newparents = array(''=>__('root',true));
		foreach($categories as $k => $v)
		{
			$newparents[$k]=$v;
		}
		$categories = $newparents;
		
		
        if (!empty($this->data)) {
        	//print_r($this->data);
            if ($this->{$modelClass}->save($this->data)) {
                $this->Session->setFlash(__('The User has been saved', true));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
            }
        }
        if (empty($this->data)) {
            $this->data = $this->{$modelClass}->read(null, $id);
        }
        
        $this->set(compact('categories'));	
        //echo "=======";print_r($this->data['Category']);	
		
    }
    
    
    function admin_list($page=1)
    {
    	$this->paginate[$this->modelClass]['order'] = $this->modelClass.'.id DESC';
		//$this->paginate[$this->modelClass]['limit'] = Configure::read('Reading.nodes_per_page');
		${$this->name} = $this->paginate($this->modelClass);
		$this->set(compact($this->name));
    }
    
    function admin_view($id) {
    	$modelClass = $this->modelClass;
		if($id)
		{
			${$modelClass} = $this->{$modelClass}->findById($id);
		}
		else
		{
			$this->redirect(array('action'=>'lists'));
		}
		$this->set($modelClass, ${$modelClass});
	}
    
	
	function index()
	{
		//echo "===app===index======";
	}
	function _getParamVars($name)
	{
		$val ='';
		if(isset($this->params['named'][$name]))
		{
			$val=$this->params['named'][$name];
		}
		elseif(isset($this->params[$name]))
		{
			$val=$this->params[$name];
		}
		return $val;
	}
	function view($slug='/')
	{
		if(empty($slug))
		{
			$slug=$this->_getParamVars('slug');			
		}
		if(empty($id))
		{
			$id=$this->_getParamVars('id');
			if(empty($id))
			{
				$id=intval($slug);
			}
		}
		$modelClass = $this->modelClass;
//		echo intval($slug);exit;
		if(!empty($slug) && $slug != strval(intval($slug)))
		{
			${$modelClass} = $this->{$modelClass}->findBySlug($slug);
		}
		elseif($id)
		{
			${$modelClass} = $this->{$modelClass}->findById($id);
		}
		else
		{
			$this->redirect(array('action'=>'lists'));
		}
		//print_r(${$modelClass});
		$current_cateid = ${$modelClass}['Category'][0]['id'];
		//seotitle  seodescription  seokeywords  
		if(empty(${$modelClass}[$modelClass]['seotitle']))
		{
			${$modelClass}[$modelClass]['seotitle']=${$modelClass}[$modelClass]['title'];
		}
		$this->set('pageTitle', ${$modelClass}[$modelClass]['seotitle']);
		$this->set('seodescription',${$modelClass}[$modelClass]['seodescription']);
		$this->set('seokeywords',${$modelClass}[$modelClass]['seokeywords']);
		$this->set('current_cateid', $current_cateid);
		$this->set($modelClass, ${$modelClass});
	}
	

	function add()
	{
		$modelClass = $this->modelClass;

		if (!empty($this->data)) {
			$this->{$modelClass}->create();
			if ($this->{$modelClass}->save($this->data)) {
				$this->Session->setFlash(__('The Menu has been saved', true));
				$this->redirect(array('action'=>'lists'));
			} else {
				$this->Session->setFlash(__('The Menu could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id)
	{
		echo "===app===edit======";
		$modelClass = $this->modelClass;
		${$modelClass} = $this->{$modelClass}->read(null, $id);
		$this->set($modelClass, ${$modelClass});
	}
	function lists($page='')
	{
		$this->paginate[$this->modelClass]['order'] = $this->modelClass.'.id DESC';
		//$this->paginate[$this->modelClass]['limit'] = Configure::read('Reading.nodes_per_page');
		${$this->name} = $this->paginate($this->modelClass);
		$this->set(compact($this->name));
	}
	
	function __message($message,$url,$seconds=3)
	{
		$this->layout = 'message';
		$this->set('message', $message);
		$this->set('url', $url);
		$this->autoRender = false;
		$this->render('message');
	}


}
?>